package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDTO
     * @return
     */
    @Transactional // 事务
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal); //套餐
        List<SetmealDish> setmealDishlist = setmealDTO.getSetmealDishes(); // 套餐菜品关系

        setmealMapper.insert(setmeal);

        long setmealId = setmeal.getId(); //返回自动生成的套餐菜品表主键id

        if (setmealDishlist != null && setmealDishlist.size() > 0) {
            setmealDishlist.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //向套餐菜品关系表插入n条数据
            setmealDishMapper.insertBatch(setmealDishlist);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        // select * from setmeal limit 0,10
        //开始分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 根据套餐id查询套餐和其对应菜品
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        //根据id查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);

        //根据套餐id查询菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        //将查询到的数据封装到VO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 套餐批量删除
     * @param ids
     */
    @Transactional //事务
    public void deleteBatch(List<Long> ids) {
        //判断当前套餐是否能够删除---是否存在起售中的套餐？？
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id); //在这里需要使用到根据id查询套餐功能的查询套餐
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //当前菜品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long id : ids) {
            setmealMapper.deleteById(id);//删除套餐数据
            setmealDishMapper.deleteBySetmealId(id);//删除套餐菜品关系表中套餐关联的菜品数据
        }
    }

    /**
     * 根据id修改套餐基本信息和对应的菜品信息
     * @param setmealVO
     */
    @Transactional //事务
    public void updateWithDishes(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);

        //修改套餐表基本信息
        setmealMapper.update(setmeal);

        //删除原有的套餐菜品关系表数据
        setmealDishMapper.deleteBySetmealId(setmealVO.getId());

        //重新插入套餐菜品关系数据
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealVO.getId());
            });
            //向套餐菜品关系表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据套餐id修改套餐销售状态
     *
     * @param status
     * @param id
     */
    public void updateStatusById(Integer status, Long id) {
        //如果是将套餐的销售状态修改为起售,则需要保证套餐里所有的菜品的销售状态也是起售 否则抛出异常
        if(status==StatusConstant.ENABLE){
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);//根据套餐id去套餐菜品关系表里找到所有的菜品
            if(setmealDishes == null || setmealDishes.size()==0) return; //这里其实可以抛出一个异常给前端 套餐里没有菜品
            setmealDishes.forEach(setmealDish -> {//再根据菜品id去菜品表里找到菜品的销售状态
                if(dishMapper.getById(setmealDish.getDishId()).getStatus()==StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);//直接调用之前写好的根据id动态修改套餐数据接口
    }
}